import Vue from 'vue'
import {
  mapMutations
} from 'vuex'
import App from './App.vue'
import '@/plugins/api'
import router from './router'
import './plugins/vant.js'
import store from './store'

// 引入vsconsole，实时查看打印日志
// import VConsole from 'vconsole/dist/vconsole.min.js'
// var vConsole = new VConsole();

Vue.config.productionTip = false


/**
 * 缓存设置
 */
let arr = []
Vue.mixin({
  beforeRouteEnter(to, from, next) {
    next(vm => {
      const toRoute = to.name
      const fromRoute = from.name
      let r = []
      arr.forEach(e => {
        r.push(e)
      })
      let routeIndex = r.indexOf(toRoute)
      // 判断历史记录里面有没有要加载的页面，如果没有的话，就缓存
      if (routeIndex != -1) {
        // 再判断离开的页面，缓存里面有没有，如果有的话，就删除
        let routeIndex2 = r.indexOf(fromRoute)
        // console.log(from)

        if (routeIndex2 != -1 && !from.meta.keepAlive) {
          r.splice(routeIndex2, routeIndex2 + 1)
          arr = r
          vm.setIncludePageNames(r)
        }
      } else {
        r.push(toRoute)
        vm.setIncludePageNames(r)
        arr = r
      }
    })
  },
  methods: {
    ...mapMutations({
      'setIncludePageNames': 'SET_INCLUDEPAGENAMES',
      'setTitle': 'SET_TITLE'
    }),
    __clearHistory() {
      arr = [];
      return Promise.resolve();
    }
  }
});

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
